using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DICE_GS;

[System.Serializable]
public class MultiListPath
{
	public List<BoardTile> tileList = new List<BoardTile>();
}

public class PathManager : MonoBehaviour {
	
	private static PathManager instance;
	public BoardTile[,] boardTiles;
	private Dictionary<int, BoardTile> boardDic = new Dictionary<int, BoardTile>();
	public int nMaxX;
	public int nMaxY;
	public int boardCount;
	public int curPathCount;
	public List<int> pathToGoal = new List<int>();
	public List<int> candidateBid = new List<int>();
	//public List<BoardTile> pathTemp = new List<BoardTile>();
	//public List < MultiListPath > paths = new List< MultiListPath >();

	Dictionary<int, List<int> > allPathToGoal = new Dictionary<int, List<int>>();
	
	public static PathManager Instance
	{
		get { return instance; }
	}
	
	void Awake()
	{
		instance = this;

	}

	public void Init()
	{
		glog.debug ("PathManager Init="+nMaxX+" "+nMaxY+"  boardcount="+boardCount);
		boardTiles = new BoardTile[nMaxX,nMaxY];
	}
	
	// Use this for initialization
	void Start () {
		glog.debug ("pathmanager start");	
	}
	
	public bool HasTile(int x, int y)
	{
		if(x<0 || y<0)
			return false;
		if(x>= nMaxX || y>= nMaxY)
			return false;
		
		if( boardTiles[x,y] != null)
			return true;
		return false;
	}
	
	public void AddBoardDic(int id, BoardTile bt)
	{
		boardDic.Add (id,bt);
	}
	
	public BoardTile GetBoardTile(int id)
	{
		if( boardDic.ContainsKey(id) )
			return boardDic[id];
		else {
			Debug.LogError("GetBoardTile Failed. id:"+id+"  is not valid");
			return null;
		}
	}
	
	public Vector3 GetTilePos(int x, int y)
	{
		return boardTiles[x,y].transform.position;
	}

	public BoardInfo GetBoardInfo(int x, int y)
	{
		return boardTiles[x,y].boardInfo;
	}
	
	public bool IsGoal(int x, int y)
	{
		return boardTiles[x,y].boardInfo.isGoal;
	}

	public void ClearPathToGoal()
	{
		pathToGoal.Clear();
		candidateBid.Clear();
		allPathToGoal.Clear();
		allPathToGoal.Add (0, new List<int>() );
	}

	public void FindPathToGoal(int nextID)
	{
		foreach( int nKey in boardDic.Keys)
		{
			if(nKey==nextID)
			{
				pathToGoal.Add(nKey);
				if( boardDic[nKey].boardInfo.pathInfo[0].forwardBoards.Count == 0)
					return;
				if( boardDic[nKey].boardInfo.pathInfo[0].forwardBoards.Count>1 )
				{
					foreach( int bid in  boardDic[nKey].boardInfo.pathInfo[0].forwardBoards)
						candidateBid.Add(bid);
					return;
				}

				FindPathToGoal( boardDic[nKey].boardInfo.pathInfo[0].forwardBoards[0] );
			}
		}
	}

//	public void FindAllPathToGoal(int index, int nextID)
//	{
//		foreach( int nKey in boardDic.Keys)
//		{
//			if(nKey==nextID)
//			{
//				allPathToGoal[index].Add(nKey);
//				if( boardDic[nKey].boardInfo.pathInfo[0].forwardBoards.Count == 0)
//					return;
//				if( boardDic[nKey].boardInfo.pathInfo[0].forwardBoards.Count>1 )
//				{
//					allPathToGoal.Add( index+1, new List<int>() );
//					return;
//				}
//				else
//					FindAllPathToGoal( index, boardDic[nKey].boardInfo.pathInfo[0].forwardBoards[0] );
//			}
//		}
//	}

}
